﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SZSLSystem.Model;
using SZSLSystem.DBUtility;
using SZSLSystem.Common;

namespace SZSLSystem.DAL
{
    /// <summary>
    /// 数据访问层：管理员
    /// </summary>
    public partial class Administrator
    {
        public Administrator()
        {

        }

        #region  Method

        /// <summary>
        ///  判断是否存在
        /// </summary>
        /// <param name="Id">管理员编号</param>
        /// <returns></returns>
        public bool Exists(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Administrator");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        ///  判断登录名是否存在
        /// </summary>
        /// <param name="UserName">管理员登录名</param>
        /// <returns></returns>
        public bool Exists(string UserName)
        {
            string strSql = "select count(*) from Administrator where UserName=@UserName";
            SqlParameter[] parameters = {
                new SqlParameter("@UserName",SqlDbType.NVarChar,30)};
            parameters[0].Value = UserName;
            return DbHelperSQL.Exists(strSql, parameters);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="UserName">登录用户名</param>
        /// <param name="UserPwd">登录密码</param>
        /// <returns></returns>
        public bool chkAdminLogin(string UserName, string UserPwd)
        {
            string strSql = "select count(*) from Administrator where UserName=@UserName and UserPassword=@UserPassword and Status=0";
            SqlParameter[] parameters = {
                new SqlParameter("@UserName",SqlDbType.VarChar,50),
                new SqlParameter("@UserPassword", SqlDbType.VarChar,50)};
            parameters[0].Value = UserName;
            parameters[1].Value = UserPwd;
            return DbHelperSQL.Exists(strSql, parameters);
        }

        /// <summary>
        /// 返回数据总数
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public int GetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) as H ");
            strSql.Append(" from Administrator ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString()));
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">管理员信息实体</param>
        /// <returns></returns>
        public int Add(SZSLSystem.Model.Administrator model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Administrator(");
            strSql.Append("[ID],[UserName],[UserPassword],[Salt],[RealName],[Sex],[Email],[Mobile],[Telphone],[QQ],[Status],[AddDate])");
            strSql.Append("values (@ID,@UserName,@UserPassword,@Salt,@RealName,@Sex,@Email,@Mobile,@Telphone,@QQ,@Status,@AddDate)");
            SqlParameter[] parameters = {
					new SqlParameter ("@ID",SqlDbType.UniqueIdentifier),
					new SqlParameter ("@UserName",SqlDbType.VarChar,50),
					new SqlParameter ("@UserPassword",SqlDbType.VarChar,50),
					new SqlParameter ("@Salt",SqlDbType.VarChar,50),
                    new SqlParameter ("@RealName",SqlDbType.VarChar,50),
					new SqlParameter ("@Sex",SqlDbType.Int,4),
					new SqlParameter ("@Email",SqlDbType.VarChar,50),
					new SqlParameter ("@Mobile",SqlDbType.VarChar,50),
					new SqlParameter ("@Telphone",SqlDbType.VarChar,50),
					new SqlParameter ("@QQ",SqlDbType.VarChar,50),
					new SqlParameter ("@Status",SqlDbType.Int,4),
					new SqlParameter ("@AddDate",SqlDbType.DateTime)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.UserName;
            parameters[2].Value = DESEncrypt.Encrypt(model.UserPassword, model.Salt);
            parameters[3].Value = model.Salt;
            parameters[4].Value = model.RealName;
            parameters[5].Value = model.Sex;
            parameters[6].Value = model.Email;
            parameters[7].Value = model.Mobile;
            parameters[8].Value = model.Telphone;
            parameters[9].Value = model.QQ;
            parameters[10].Value = model.Status;
            parameters[11].Value = model.AddDate;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新管理员
        /// </summary>
        /// <param name="model">管理员信息实体</param>
        /// <returns></returns>
        public bool Update(SZSLSystem.Model.Administrator model)
        {
            SZSLSystem.Model.Administrator curModel = GetModel(model.UserName);
            if (curModel == null)
            {
                return false;
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Administrator set ");
            strSql.Append("[ID]=@ID,");
            strSql.Append("[UserName]=@UserName,");
            strSql.Append("[UserPassword]=@UserPassword,");
            strSql.Append("[Salt]=@Salt,");
            strSql.Append("[RealName]=@RealName,");
            strSql.Append("[Sex]=@Sex,");
            strSql.Append("[Email]=@Email,");
            strSql.Append("[Mobile]=@Mobile,");
            strSql.Append("[Telphone]=@Telphone,");
            strSql.Append("[QQ]=@QQ,");
            strSql.Append("[Status]=@Status,");
            strSql.Append("[AddDate]=@AddDate");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter ("@ID",SqlDbType.UniqueIdentifier),
					new SqlParameter ("@UserName",SqlDbType.VarChar,50),
					new SqlParameter ("@UserPassword",SqlDbType.VarChar,50),
					new SqlParameter ("@Salt",SqlDbType.VarChar,50),
                    new SqlParameter ("@RealName",SqlDbType.VarChar,50),
					new SqlParameter ("@Sex",SqlDbType.Int,4),
					new SqlParameter ("@Email",SqlDbType.VarChar,50),
					new SqlParameter ("@Mobile",SqlDbType.VarChar,50),
					new SqlParameter ("@Telphone",SqlDbType.VarChar,50),
					new SqlParameter ("@QQ",SqlDbType.VarChar,50),
					new SqlParameter ("@Status",SqlDbType.Int,4),
					new SqlParameter ("@AddDate",SqlDbType.DateTime)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.UserName;
            parameters[2].Value = model.UserPassword.Trim() == "" ? curModel.UserPassword 
                : DESEncrypt.Encrypt(model.UserPassword, curModel.Salt);
            parameters[3].Value = model.Salt;
            parameters[4].Value = model.RealName;
            parameters[5].Value = model.Sex;
            parameters[6].Value = model.Email;
            parameters[7].Value = model.Mobile;
            parameters[8].Value = model.Telphone;
            parameters[9].Value = model.QQ;
            parameters[10].Value = model.Status;
            parameters[11].Value = model.AddDate;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除管理员
        /// </summary>
        /// <param name="Id">管理员编号</param>
        /// <returns></returns>
        public bool Delete(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Administrator ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.UniqueIdentifier)
            };
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据管理员登录名获取数据
        /// </summary>
        /// <param name="userName">管理员登录名</param>
        /// <returns></returns>
        public SZSLSystem.Model.Administrator GetModel(string UserName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from Administrator");
            strSql.Append(" where UserName=@UserName ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)};
            parameters[0].Value = UserName;

            SZSLSystem.Model.Administrator model = new SZSLSystem.Model.Administrator();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow row = ds.Tables[0].Rows[0];
                if (row["ID"].ToString() != "")
                {
                    model.ID = (Guid)row["ID"];
                }
                model.UserName = row["UserName"].ToString();
                model.UserPassword = row["UserPassword"].ToString();
                model.Salt = row["Salt"].ToString();
                model.RealName = row["RealName"].ToString();
                if (row["Sex"].ToString() != "")
                {
                    model.Sex = int.Parse(row["Sex"].ToString());
                }
                model.Email = row["Email"].ToString();
                model.Mobile = row["Mobile"].ToString();
                model.Telphone = row["Telphone"].ToString();
                model.QQ = row["QQ"].ToString();
                if (row["Status"].ToString() != "")
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
                if (row["AddDate"].ToString() != "")
                {
                    model.AddDate = DateTime.Parse(row["AddDate"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 根据管理员编号编号获取数据
        /// </summary>
        /// <param name="Id">管理员编号</param>
        /// <returns></returns>
        public SZSLSystem.Model.Administrator GetModel(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from Administrator ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.UniqueIdentifier)
            };
            parameters[0].Value = Id;

            SZSLSystem.Model.Administrator model = new SZSLSystem.Model.Administrator();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow row = ds.Tables[0].Rows[0];
                if (row["ID"].ToString() != "")
                {
                    model.ID = (Guid)row["ID"];
                }
                model.UserName = row["UserName"].ToString();
                model.UserPassword = row["UserPassword"].ToString();
                model.Salt = row["Salt"].ToString();
                model.RealName = row["RealName"].ToString();
                if (row["Sex"].ToString() != "")
                {
                    model.Sex = int.Parse(row["Sex"].ToString());
                }
                model.Email = row["Email"].ToString();
                model.Mobile = row["Mobile"].ToString();
                model.Telphone = row["Telphone"].ToString();
                model.QQ = row["QQ"].ToString();
                if (row["Status"].ToString() != "")
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
                if (row["AddDate"].ToString() != "")
                {
                    model.AddDate = DateTime.Parse(row["AddDate"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得管理员数据列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [ID],[UserName],[UserPassword],[Salt],[RealName],[Sex],[Email],[Mobile],[Telphone],[QQ],[Status],[AddDate] ");
            strSql.Append(" from Administrator ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by AddDate desc");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得管理员前几行数据
        /// </summary>
        /// <param name="Top">行数</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="filedOrder">排序条件</param>
        /// <returns></returns>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" [ID],[UserName],[UserPassword],[Salt],[RealName],[Sex],[Email],[Mobile],[Telphone],[QQ],[Status],[AddDate] ");
            strSql.Append(" from Administrator ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得管理员查询分页数据
        /// </summary>
        /// <param name="pageSize">每页数目</param>
        /// <param name="currentPage">当前页数</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="filedOrder">排序条件</param>
        /// <returns></returns>
        public DataSet GetPageList(int pageSize, int currentPage, string strWhere, string filedOrder)
        {
            int topSize = pageSize * currentPage;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top " + pageSize + " [ID],[UserName],[UserPassword],[Salt],[RealName],[Sex],[Email],[Mobile],[Telphone],[QQ],[Status],[AddDate] from Administrator");
            strSql.Append(" where Id not in(select top " + topSize + " Id from Administrator");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder + ")");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);

            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}
